using DOTS.DOD.LESSON3;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;

namespace DOTS.DOD.LESSON5
{
    [BurstCompile]
    partial struct GenerateCubesWithParallelWriterJob : IJobFor
    {
        [ReadOnly] public Entity CubeProtoType;
        public NativeArray<Entity> Cubes;
        public EntityCommandBuffer.ParallelWriter ECB_Parallel;
        [NativeDisableUnsafePtrRestriction] public RefRW<RandomSingleton> Random;

        public void Execute(int index)
        {
            Cubes[index] = ECB_Parallel.Instantiate(index, CubeProtoType);

            ECB_Parallel.AddComponent(index, Cubes[index], new RotateAndMoveSpeed()
            {
                RotateSpeed = math.radians(60f),
                MoveSpeed = 5f,
            });

            float2 targetPos2 = Random.ValueRW.Rand.NextFloat2(new float2(-15f, -15f), new float2(15f, 15f));
            ECB_Parallel.AddComponent(index, Cubes[index], new RandomTarget()
            {
                TargetPos = new float3(targetPos2.x, 0, targetPos2.y),
            });
        }
    }
}